package com.csht.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csht.model.CshtUser;
import com.csht.page.PagerModel;
import com.csht.page.SystemContext;
import com.csht.service.CshtGoodsService;
import com.csht.service.CshtTradingSuccessService;
import com.csht.vo.my.CshtTradingSuccessVo;

/**
 * 已出售的商品controller
 * @author chenhuiyan
 *
 */
@Controller
@RequestMapping(value = "/toMy")
public class TrandingSuccessController {
	
	@Autowired
	private CshtTradingSuccessService cshtTradingSuccessService;
	
	@Autowired
	private CshtGoodsService cshtGoodsService;
	
	/**
	 * 个人中心－已出售的商品页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/tradingSuccess")
	public String tradingSuccess(ModelMap model, HttpServletRequest request) throws Exception {
		//获取登录的用户
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser");
		Map<String, Integer> res = new HashMap<String, Integer>();
		res.put("userId", cshtUser.getUserId());
		res.put("messagePageSize", SystemContext.getMessagePageSize());
		res.put("offset", SystemContext.getOffset());
		PagerModel<CshtTradingSuccessVo> pagerModel = cshtTradingSuccessService.getTradingSuccessByUserId(res);
		model.addAttribute("pagerModel", pagerModel);
		return "/toMy/tradingSuccess";
	}
	
	/**
	 * 继续上架已出售的商品
	 * @param tradingSuccessId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/tradingSuccessToSell")
	public Map<String, Object> tradingSuccessToSell(@RequestParam(name = "tradingSuccessId") Integer tradingSuccessId) 
			throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("result", false);
		Integer goodsId = cshtTradingSuccessService.selectTradingSuccessById(tradingSuccessId); //查到原商品id
		cshtTradingSuccessService.deleteTradingSuccessById(tradingSuccessId); //删除需要继续上架的商品
		cshtGoodsService.updateGoodsStateSellById(goodsId); //更新商品为上架状态
		res.put("result", true);
		return res;
	}

}
